﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tuwien.auto.eibclient;

namespace KNXFramework.KNXObjects.ValueObjects
{
    public class StepDirObjectValue : IValueObject
    {
        #region Fields
        private int m_Direction;
        private int m_StepCode;

        #endregion

        protected int Direction
        {
            get { return m_Direction; }
            set { m_Direction = value; }
        }
        protected int StepCode
        {
            get { return m_StepCode; }
            set { m_StepCode = value; }
        }


        #region Constructors
        protected StepDirObjectValue(int dir, int step)
        {
            m_Direction = dir;
            m_StepCode = step;
        }
        public StepDirObjectValue()
        {

        }
        public StepDirObjectValue(EIBBuffer buffer)
        {
            int len = buffer.data.Length;
            int newValue;
            if (len == 2)
                newValue = buffer.data[1] & 0x3f;
            else
                newValue = buffer.data[2];

            int direction = (newValue & 0x08) >> 3;
            int stepCode = newValue & 0x07;
            if (stepCode == 0)
                direction = 0;

            StepCode = stepCode;
            Direction = direction;
        }
        #endregion

        #region IValueObject Members

        public void AdaptBufferBeforeSending(bool isWrite, EIBBuffer buffer)
        {
            buffer.data[1] = Convert.ToByte(buffer.data[1] | ((Direction == 1 ? 8 : 0) | (StepCode & 0x07)));
        }
        #endregion

        #region IEquatable<IValueObject> Members

        public bool Equals(IValueObject other)
        {
            bool equality = false;
            StepDirObjectValue typedOther = other as StepDirObjectValue;
            if (typedOther != null)
            {
                equality = m_Direction == typedOther.Direction && StepCode == typedOther.StepCode;
            }
            return equality;
        }

        #endregion
    }
}
